package com.ruoyi.wx;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.wx.util.AddressResponse;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class GeocoderRestTemplate {
    public static void main(String[] args) throws JsonProcessingException {
        String address = "安阳市北关区人民大道";
        String key = "TGPBZ-MUFL4-I54U7-KWAA3-K4MCE-EXB22";
        String url = "https://apis.map.qq.com/ws/geocoder/v1?address=" +
                     URLEncoder.encode(address, StandardCharsets.UTF_8) +
                     "&key=" + key;

        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(URI.create(url), String.class);
        System.out.println(response);

        ObjectMapper mapper = new ObjectMapper();
        AddressResponse response1 = mapper.readValue(response, AddressResponse.class);


// 使用对象
        System.out.println(response1.getResult().getLocation().getLat());
        System.out.println(response1.getResult().getLocation().getLng());

    }
}